package com.songlanyun.modules.information.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.modules.information.dao.InformationClassifyDao;
import com.songlanyun.modules.information.entity.InformationClassifyEntity;
import com.songlanyun.modules.information.entity.InformationEntity;
import com.songlanyun.modules.information.model.dto.InformationClassifyDto;
import com.songlanyun.modules.information.service.InformationClassifyService;
import com.songlanyun.modules.information.service.InformationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service("informationClassifyService")
public class InformationClassifyServiceImpl extends ServiceImpl<InformationClassifyDao, InformationClassifyEntity> implements InformationClassifyService {

    @Autowired
    private InformationService informationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = MapUtil.getStr(params, "name");
        String startTime = MapUtil.getStr(params, "start_time");
        String endTime = MapUtil.getStr(params, "end_time");
        IPage<InformationClassifyEntity> page = this.page(
                new Query<InformationClassifyEntity>().getPage(params),
                new LambdaQueryWrapper<InformationClassifyEntity>()
                        .like(StringUtils.isNotBlank(name), InformationClassifyEntity::getName, name)
                        .ge(StringUtils.isNotBlank(startTime), InformationClassifyEntity::getCreateTime, startTime)
                        .le(StringUtils.isNotBlank(endTime), InformationClassifyEntity::getCreateTime, endTime)
                        .orderByAsc(InformationClassifyEntity::getSort)
                        .orderByDesc(InformationClassifyEntity::getCreateTime)
        );

        return new PageUtils(page);
    }

    @Override
    public InformationClassifyEntity getById(Long id, boolean tw) {
        InformationClassifyEntity classifyEntity = this.baseMapper.selectById(id);
        if (ObjectUtil.isNull(classifyEntity) && tw) {
            throw new RRException("资讯分类不存在");
        }
        return classifyEntity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Long userId, InformationClassifyDto dto) {
        InformationClassifyEntity classifyEntity = BeanUtil.toBean(dto, InformationClassifyEntity.class);
        classifyEntity.setCreateId(userId);
        classifyEntity.setUpdateId(userId);
        this.baseMapper.insert(classifyEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(Long userId, InformationClassifyDto dto) {
        InformationClassifyEntity classifyEntity = this.getById(dto.getId(), true);
        BeanUtil.copyProperties(dto, classifyEntity);
        classifyEntity.setUpdateId(userId);
        this.baseMapper.updateById(classifyEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        Integer count = informationService.lambdaQuery().eq(InformationEntity::getClassifyId, id).count();
        if (count > 0) {
            throw new RRException("该资讯分类下存在内容，不能删除");
        }
        this.baseMapper.deleteById(id);
    }

}
